// pages/advert/module/index.js
import config from '../../../config/index.js'
import wxRequest from '../../../utils/wxRequest.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1, //当前页
    numbers: 20, //一页显示多少条
    total: 0, //总条数
    datalist: [],
    headArr: ['模板名称', '模板备注']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getList();
  },
  //获取模板列表
  getList() {
    var map = {
      'page': this.data.page,
      'number': this.data.numbers,
    }
    wxRequest.getRequest('/toolkit/templates/index', map).then(res => {
      console.log(res)
      this.setData({
        datalist: res.data.data,
        total: res.data.total
      })
    }).catch(error => {})
  },
  //上拉加载数据
  loadlist() {
    var map = {
      'page': this.data.page + 1, //下一页
      'number': this.data.numbers,
    }
    var newsBefore = this.data.datalist; //旧数据
    // console.log(map)
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 500
    })
    wxRequest.getRequest('/toolkit/templates/index', map).then(res => {
      if (res.data.data != '') {
        this.setData({
          page: map.page,
          total: res.data.total,
          datalist: newsBefore.concat(res.data.data),
        })
      } else {
        wx.showToast({
          title: '数据已加载完',
          duration: 1000
        })
      }

    }).catch(error => {})
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    that.setData({
      page: 1,
    })
    that.getList();
    wx.showToast({
      title: '刷新成功',
      duration: 1000
    })
    wx.setBackgroundTextStyle({
      textStyle: 'dark', // 下拉背景字体、loading 图的样式为dark
    })
    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();
    // 停止下拉动作  
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    that.loadlist();
  }
})